package com.hqyj.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.QuestionMapper;
import com.hqyj.pojo.Exampaper;
import com.hqyj.pojo.Question;
import com.hqyj.service.QuestionService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-11-12
 */
@Service
public class QuestionServiceImpl implements QuestionService {
    /**
     * 注入QuestionMapper 接口
     */
    @Autowired
    private QuestionMapper questionMapper;

    /**
     * 注入Redis工具类
     * */
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public HashMap<String, Object> getAllQuestion(Question question) {
        HashMap<String, Object> map = new HashMap<>();
        // 创建分页对象
        Page<Question> questionPage = new Page<>(question.getPage(),question.getRow());

        QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>();
        // 获取所有题目信息
        // List<Question> questionList = questionMapper.selectList(questionQueryWrapper);

        //if (questionList.size() > 0) {
        //    map.put("info", "success");
        //    map.put("list", questionList);
        //} else {
        //    map.put("info", "error");
        //}

        // 定义当前页 的 key
        String curKey = "question:list:"+question.getPage() + ":" + question.getRow();
        // 定义总条数 key
        String totalKey = "question:total";

        // 判断当前页的 数据 是否在 Redis 缓存中
        Object obj = redisTemplate.opsForValue().get(curKey);
        if (obj!=null){
            map.put("info", "success");
            map.put("list", redisTemplate.opsForValue().get(curKey));
            map.put("total", redisTemplate.opsForValue().get(totalKey));
        } else {
            Page<Question> page = questionMapper.selectPage(questionPage,questionQueryWrapper);
            map.put("info", "success");
            map.put("list", page.getRecords());
            map.put("total", page.getTotal());

            // 把数据存放到 Redis 中
            redisTemplate.opsForValue().set(curKey,page.getRecords(),60, TimeUnit.SECONDS);
            redisTemplate.opsForValue().set(totalKey,page.getTotal(),60,TimeUnit.SECONDS);
        }

        return map;
    }

    @Override
    public HashMap<String, Object> list(Question q) {
        HashMap<String, Object> map = new HashMap<>();
        //创建分页对象
        System.out.println("======================"+q);
        Page<Question>  page = new Page<>(q.getPage(),q.getRow());

        // 条件构造器
        QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>();
        // 条件查询
        questionQueryWrapper.eq(!StringUtils.isEmpty(q.getQuestionDifficulty()) &&  q.getQuestionDifficulty()!=null,"question_difficulty",q.getQuestionDifficulty());
        questionQueryWrapper.like(
                (!StringUtils.isEmpty(q.getQuestionTopic())) && q.getQuestionTopic()!=null,
                "question_topic",q.getQuestionTopic());
        questionQueryWrapper.like(
                (!StringUtils.isEmpty(q.getQuestionType())) && q.getQuestionType()!=null,
                "question_type",q.getQuestionType());
        questionQueryWrapper.like(
                (!StringUtils.isEmpty(q.getQuestionCourse())) && q.getQuestionCourse()!=null,
                "question_course",q.getQuestionCourse());

        questionQueryWrapper.orderByDesc("question_update","question_id");

        //获取分页结果
        Page p = questionMapper.selectPage(page,questionQueryWrapper);
        //分页结构集
        map.put("list",p.getRecords());
        //总条数
        map.put("total",p.getTotal());


        return map;
    }

    @Override
    public HashMap<String, Object> update(Question q) {
        HashMap<String, Object> map = new HashMap<>();

        // 条件构造器
        QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>();
        //判断是否题目重名
        questionQueryWrapper.eq(q.getQuestionTopic()!=null && q.getQuestionTopic()!="","question_topic",q.getQuestionTopic());
        List<Question> questionList = questionMapper.selectList(questionQueryWrapper);

        if(questionList.size()==0){
            q.setQuestionUpdate(new Date());
            int num = questionMapper.updateById(q);
            if(num>0){
                map.put("info","保存成功");
            }else {
                map.put("info","保存失败");
            }
        }else {
            map.put("info","题目重名，保存失败");
        }

        return map;
    }

    @Override
    public HashMap<String, Object> add(Question q) {
        HashMap<String, Object> map = new HashMap<>();
        // 条件构造器
        QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>();
        //判断是否题目重名
        questionQueryWrapper.eq(q.getQuestionTopic()!=null && q.getQuestionTopic()!="","question_topic",q.getQuestionTopic());
        List<Question> questionList = questionMapper.selectList(questionQueryWrapper);

        if(questionList.size()==0){
            q.setQuestionUpdate(new Date());
            int num = questionMapper.insert(q);
            if(num>0){
                map.put("info","保存成功");
            }else {
                map.put("info","保存失败");
            }
        }else {
            map.put("info","题目重名，保存失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> delete(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        int num = questionMapper.deleteById(id);
        if(num>0){
            map.put("info","删除成功");
        }else {
            map.put("info","删除失败");
        }
        return map;
    }
}
